#!/usr/bin/perl -w

use Image::ExifTool ':Public';
use Math::Trig;

my $file = shift or die "Please enter filename";
my $info = ImageInfo($file);

if ($info->{FOV}) {
    $info->{FOV} =~ /([\d.]+)/;
    print $1, "\n";
    exit;
}

if ($info->{FocalLength35efl}) {
    $info->{FocalLength35efl} =~ /[\d\.]+\D+[\d\.]+\D+([\d\.]+)/;
    print &calcFOV($1), "\n";
    exit;
}

if ($info->{FocalLength}) {
    $info->{FocalLength} =~ /([\d\.]+)/;
    $flength = $1;
    $widths[0] = 36;
    if ($info->{Make}) {
        $make = $info->{Make};

        # Based on http://en.wikipedia.org/wiki/APS-C
        if ($make =~ /Canon/i) {
            push(@widths, 22.2); #Canon APS-C, 1.62x
            push(@widths, 27.9); #Canon APS-H, 1.25x
            push(@widths, 20.7); #Canon EOS DCS 3, 1.7x
        } elsif ($make =~ /Nikon/i) {
            push(@widths, 23.7); #Nikon APS-C, 1.52x
        } elsif ($make =~ /Sigma/i) {
            push(@widths, 20.7); #Sigma Foveon, 1.7x
        } elsif ($make =~ /Pentax/i) {
            push(@widths, 23.5); #Pentax, compromise b/w 1.53 and 1.54
        } elsif ($make =~ /Fuji/i) {
            push(@widths, 23.7);
        } elsif ($make =~ /Konica Minolta/i) {
            push(@widths, 23.7);
        } elsif ($make =~ /Sony/i) {
            push(@widths, 23.7);
        } elsif ($make =~ /Kodak/i) {
            push(@widths, 27.9);
        }

    }
        
    foreach(@widths) {
        print &calcFOV($flength, $_), "\n";
    }
    exit;
}

print &calcFOV, "\n";
exit;


sub calcFOV {
    my ($length, $width);
    $length = 50;
    $width = 36;

    $length = $_[0] unless @_<1;
    $width = $_[1] unless @_<2;

    rad2deg(2 * atan($width / (2 * $length)));

}
